var UserList = function() {

    var initPickers = function() {
        //init date pickers
        $('.date-picker').datepicker({
            rtl: Metronic.isRTL(),
            language: 'vi',
            autoclose: true
        });
    }

    var handleOrders = function() {

        var table = $("#datatable_list");
        var grid = new Datatable();
        grid.init({
            src: table,
            onSuccess: function(grid) {
                // execute some code after table records loaded
            },
            onError: function(grid) {
                // execute some code on network or other general error  
            },
            dataTable: {// here you can define a typical datatable settings from http://datatables.net/usage/options 
                'sAjaxSource': table.attr('action'),
                "aoColumns": [
                    {
                        "mDataProp": "checkbox",
                        "sClass": "no-wrap"
                    },
                    {
                        "mDataProp": "id",
                        "sClass": "no-wrap"
                    },
                    {
                        "mDataProp": "username",
                        "sClass": "no-wrap"
                    },
                    {
                        "mDataProp": "birthday",
                        "sClass": "no-wrap"
                    },
                    {
                        "mDataProp": "phone",
                        "sClass": "no-wrap"
                    },
                    {
                        "mDataProp": "actions",
                        "sClass": "no-wrap"
                    },
                ],
                "aoColumnDefs": [
                    {
                        'bSortable': false,
                        'aTargets': [0, 5]
                    }
                ],
                "bServerSide": true
            }
        });

        // handle filter
        grid.getDataTable().on('change', '.form-filter[name^="filter_"]', function(e) {
            $('.filter-submit', grid.getDataTable()).trigger('click');
        });

        $('.filter-submit', grid.getDataTable()).trigger('click');

        grid.getTableWrapper().on('change', '.table-group-action-input', function(e) {
            $(this).parent().find('.setclass').hide();
            $(this).parent().find('.assign_permission').hide();

            if ($(this).val() == 'setclass') {
                $(this).parent().find('.setclass').show();
            }
            else if ($(this).val() == 'assign_permission') {
                $(this).parent().find('.assign_permission').show();
            }
        });

        // handle group actionsubmit button click
        grid.getTableWrapper().on('click', '.table-group-action-submit', function(e) {
            e.preventDefault();
            var action = $(".table-group-action-input", grid.getTableWrapper());
            if (action.val() != "" && grid.getSelectedRowsCount() > 0) {
                grid.addAjaxParam("sAction", "group_action");
                grid.addAjaxParam("sGroupActionName", action.val());
                var records = grid.getSelectedRows();
                for (var i in records) {
                    grid.addAjaxParam(records[i]["name"], records[i]["value"]);
                }

                if (action.val() == 'setclass') {
                    grid.addAjaxParam('class_id', $('.setclass', grid.getTableWrapper()).val());
                }
                else if (action.val() == 'assign_permission') {
                    grid.addAjaxParam('role_id', $('.assign_permission', grid.getTableWrapper()).val());
                }

                grid.getDataTable().fnStandingRedraw();
                grid.removeAjaxParam('sAction');
                grid.removeAjaxParam('sGroupActionName');
                grid.removeAjaxParam('class_id');
                for (var i in records) {
                    grid.removeAjaxParam(records[i]["name"]);
                }
            } else if (action.val() == "") {
                Metronic.alert({
                    type: 'danger',
                    icon: 'warning',
                    message: $('input:hidden.error-no-action').val(),
                    container: grid.getTableWrapper(),
                    place: 'prepend'
                });
            } else if (grid.getSelectedRowsCount() === 0) {
                Metronic.alert({
                    type: 'danger',
                    icon: 'warning',
                    message: $('input:hidden.error-no-item').val(),
                    container: grid.getTableWrapper(),
                    place: 'prepend'
                });
            }
        });

    }

    return {
        //main function to initiate the module
        init: function() {
            initPickers();
            handleOrders();
        }

    };

}();